package com.yfbao.horizon.schedule.tk.utils;

import cn.hutool.core.io.FileUtil;
import lombok.extern.slf4j.Slf4j;
import toolgood.words.StringSearchEx2;
import toolgood.words.WordsSearchEx2;
import toolgood.words.WordsSearchResult;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
public class ToolgoodWordUtil {

    private static List<String> keyWordList = new ArrayList<>();
    private static List<String> textList = new ArrayList<>();

    public static void main(String[] args) {
        initKeyWord();
        initTextList();
//        testWordsSearchExMemory();
//        testStringSearchEx2Memory();

        stringFindAll(textList);
        word2FindAll(textList);
    }

    private static void initKeyWord(){
//        keyWordList = FileUtil.readLines("dict_20231117.txt", "utf-8");
        keyWordList = FileUtil.readLines("dict_1W.txt", "utf-8");
    }

    private static void initTextList(){
        List<String> stringList = FileUtil.readLines("测试文本.txt", "utf-8");
        int countNum = 10000;
        for(int i=0;i<countNum;i++){
            textList.addAll(stringList);
        }
        log.info("total text:"+textList.size());
    }

    private static void stringFindAll(List<String> list){
        StringSearchEx2 searchEx2 = new StringSearchEx2();
        searchEx2.SetKeywords(keyWordList);

        long start = System.currentTimeMillis();
        for (String s : list) {
            List<String> stringList = searchEx2.FindAll(s);
//            log.info("检索的敏感词："+stringList);

            String replace = searchEx2.Replace(s, '*');
//            log.info("替换敏感词后的结果："+replace);
        }
        log.info("total cost:"+(System.currentTimeMillis()-start)+"ms");
    }

    private static void word2FindAll(List<String> list){
        WordsSearchEx2 wordsSearchEx2 = new WordsSearchEx2();
        wordsSearchEx2.SetKeywords(keyWordList);

        long start = System.currentTimeMillis();
        for (String s : list) {
            List<WordsSearchResult> wordsSearchResults = wordsSearchEx2.FindAll(s);
            List<String> collect = wordsSearchResults.stream().map(str -> str.Keyword).collect(Collectors.toList());
//            log.info("检索的敏感词："+collect);

            String replace = wordsSearchEx2.Replace(s, '*');
//            log.info("替换敏感词后的结果："+replace);
        }

        log.info("total cost:"+(System.currentTimeMillis()-start)+"ms");
    }

    private static void testWordsSearchExMemory(){
        log.info("begin test memory");
        long totalMemory = Runtime.getRuntime().totalMemory();
        log.info("Total Memory: " + totalMemory / (1024 * 1024) + " MB");
        Map<String, WordsSearchEx2> map = new HashMap<>();
        long start = System.currentTimeMillis();
        int total=4;
        for(int i=0;i<total;i++){
            WordsSearchEx2 wordsSearchEx2 = new WordsSearchEx2();
            wordsSearchEx2.SetKeywords(keyWordList);
            map.put(String.valueOf(i),wordsSearchEx2);
            if(i==(total/2)){
                try {
                    long totalMemory1 = Runtime.getRuntime().totalMemory();
                    log.info((int)(total/2)+", Total Memory: " + totalMemory1 / (1024 * 1024) + " MB");
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        long totalMemory2 = Runtime.getRuntime().totalMemory();
        log.info(total+", Total Memory: " + totalMemory2 / (1024 * 1024) + " MB");
        log.info(total+", Total cost: " + (System.currentTimeMillis()-start)+"ms");
        log.info("finish all memory");
    }


    private static void testStringSearchEx2Memory(){
        log.info("begin test memory");
        long totalMemory = Runtime.getRuntime().totalMemory();
        log.info("Total Memory: " + totalMemory / (1024 * 1024) + " MB");
        Map<String, StringSearchEx2> map = new HashMap<>();
        long start = System.currentTimeMillis();
        int total=4;
        for(int i=0;i<total;i++){
            StringSearchEx2 searchEx2 = new StringSearchEx2();
            searchEx2.SetKeywords(keyWordList);
            map.put(String.valueOf(i),searchEx2);
            if(i==(total/2)){
                try {
                    long totalMemory1 = Runtime.getRuntime().totalMemory();
                    log.info((int)(total/2)+", Total Memory: " + totalMemory1 / (1024 * 1024) + " MB");
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        long totalMemory2 = Runtime.getRuntime().totalMemory();
        log.info(total+", Total Memory: " + totalMemory2 / (1024 * 1024) + " MB");
        log.info(total+", Total cost: " + (System.currentTimeMillis()-start)+"ms");
        log.info("finish all memory");
    }
}
